package xyz.bali16.module.activeuser.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import xyz.bali16.module.activeuser.entity.UserActiveUser;
import xyz.bali16.module.activeuser.mapper.UserActiveUserMapper;

import java.util.Collections;
import java.util.List;


@Service
@AllArgsConstructor
public class UserActiveUserServiceImpl extends ServiceImpl<UserActiveUserMapper, UserActiveUser> implements UserActiveUserService {

    @Override
    public void add(String userId, String activeUserId) {
        if (activeUserId == null) {
            return;
        }
        //List<UserActiveUser> userActiveUsers = activeUserIds.stream()
        //        .filter(activeUserId -> !StrUtil.isEmpty(activeUserId))
        //        .map(activeUserId -> {
        UserActiveUser userActiveUser = new UserActiveUser();
        userActiveUser.setUserId(userId);
        userActiveUser.setActiveUserId(activeUserId);

        //})
        //.collect(Collectors.toList());

        save(userActiveUser);
    }

    @Override
    public void removeByActiveUserId(String activeUserId) {
        if (StrUtil.isEmpty(activeUserId)) {
            return;
        }
        Wrapper<UserActiveUser> wrapper = Wrappers.<UserActiveUser>lambdaQuery()
                .eq(UserActiveUser::getActiveUserId, activeUserId);
        remove(wrapper);
    }

    @Override
    public void removeByUserId(String userId) {
        if (StrUtil.isEmpty(userId)) {
            return;
        }
        Wrapper<UserActiveUser> wrapper = Wrappers.<UserActiveUser>lambdaQuery()
                .eq(UserActiveUser::getUserId, userId);
        remove(wrapper);
    }

    @Override
    public void removeByActiveUserId(String userId,String activeUserId) {
        if (StrUtil.isEmpty(userId)) {
            return;
        }
        if (activeUserId == null) {
            return;
        }
        Wrapper<UserActiveUser> wrapper = Wrappers.<UserActiveUser>lambdaQuery()
                .eq(UserActiveUser::getUserId, userId)
                .eq(UserActiveUser::getActiveUserId, activeUserId);
        remove(wrapper);
    }

    @Override
    public UserActiveUser getByUserId(String userId) {
        Wrapper<UserActiveUser> wrapper = Wrappers.<UserActiveUser>lambdaQuery()
                .eq(UserActiveUser::getUserId, userId);
        return getOne(wrapper);
    }

    @Override
    public List<UserActiveUser> getByActiveUserId(String activeUserId) {
        if (StrUtil.isEmpty(activeUserId)) {
            return Collections.emptyList();
        }
        Wrapper<UserActiveUser> wrapper = Wrappers.<UserActiveUser>lambdaQuery()
                .eq(UserActiveUser::getActiveUserId, activeUserId);
        return list(wrapper);
    }


}
